import React, { Component } from 'react'
import store from '../redux/store.js';

export default class Count extends Component {
  // count = store.getState();
  state = {
    count: store.getState()
  }
  // 监测状态变化
  componentDidMount = () => {
    store.subscribe(() => {
      this.setState({
        count: store.getState()
      })
    })
  }

  add = (e) => {
    console.log('count', this.state.count);
    
    store.dispatch({ type: 'increment', data: e.detail*1 })
  }
  
  render() {
    return (
      <div>
        <h2>当前求和为：{this.state.count}</h2>
        <button onClick={this.add}> +1 </button>
        <button onClick={e => this.store.dispatch({ type: 'minus' })}> -1 </button>
      </div>
    )
  }
}
